﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;
using AutoMapper;

namespace Data
{
   public class ChiTietHoaDonRepository
    {
       private static ChiTietHoaDonDTO MapChiTietHoaDonEFToChiTietHoaDonDTO(CHITIETHOADON chitiethoadonEF)
       {
           Mapper.CreateMap<CHITIETHOADON, ChiTietHoaDonDTO>();
           return Mapper.Map<CHITIETHOADON, ChiTietHoaDonDTO>(chitiethoadonEF);
       }
       private static CHITIETHOADON MapChiTierHoaDonDTOToChiTietHoaDonEF(ChiTietHoaDonDTO chitiethoadonDTO)
       {
           Mapper.CreateMap<ChiTietHoaDonDTO, CHITIETHOADON>();
           return Mapper.Map<ChiTietHoaDonDTO, CHITIETHOADON>(chitiethoadonDTO);
       }
       public static List<ChiTietHoaDonDTO> GetChiTietHoaDonByMaHoaDon(int mahoadon)
       {
           List<ChiTietHoaDonDTO> list = new List<ChiTietHoaDonDTO>();
           using (QuanLyNhaSachEntities context = new QuanLyNhaSachEntities())
           {
               try
               {
                   var query = from chitiethoadon in context.CHITIETHOADONList
                               where chitiethoadon.MaHoaDon == mahoadon
                               select chitiethoadon;
                   foreach(var chitiethoadonEF in query)
                   {
                       ChiTietHoaDonDTO chitiethoadonDTO = MapChiTietHoaDonEFToChiTietHoaDonDTO(chitiethoadonEF);
                       list.Add(chitiethoadonDTO);
                   }

               }
               catch(Exception)
               {
               }
           }
           return list;
       }
       public void Insert(ChiTietHoaDonDTO chitiethoadonDTO)
       {
           using (QuanLyNhaSachEntities context = new QuanLyNhaSachEntities())
           {
               try
               {
                   CHITIETHOADON chitiethoadonEF = new CHITIETHOADON();
                   chitiethoadonEF = MapChiTierHoaDonDTOToChiTietHoaDonEF(chitiethoadonDTO);
                   context.AddToCHITIETHOADONList(chitiethoadonEF);
                   context.SaveChanges();
               }
               catch (Exception)
               {

               }
           }
       }
       public void Update(ChiTietHoaDonDTO chitiethoadonDTO)
       {
           using (QuanLyNhaSachEntities context = new QuanLyNhaSachEntities())
           {
               try
               {
                   EntityKey key = context.CreateEntityKey("CHITIETHOADONList", new CHITIETHOADON { MaHoaDon = chitiethoadonDTO.MaHoaDon });
                   CHITIETHOADON origchitiethoadon = context.GetObjectByKey(key) as CHITIETHOADON;
                   context.Detach(origchitiethoadon);
                   context.Attach(origchitiethoadon);
                   CHITIETHOADON updatechitiethoadon = new CHITIETHOADON();
                   updatechitiethoadon = MapChiTierHoaDonDTOToChiTietHoaDonEF(chitiethoadonDTO);
                   context.ApplyCurrentValues<CHITIETHOADON>("CHITIETHOADONList", updatechitiethoadon);
                   context.SaveChanges();
               }
               catch (Exception)
               {

               }
           }
       }
    }
}
